<?php

/**
 * 上传处理类 Upload.class.php
 * ============================================================================
 * 版权所有 (C) 2015-2016 壹尚科技有限公司，并保留所有权利。
 * 网站地址:   http://www.ethank.com.cn
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * Author: 勾国印 (gouguoyin@ethank.com.cn) 
 * Date: 2015年7月25日 下午4:21:40  
*/

namespace Common\Lib;
class Upload{
    private $upload_pic_size; // 允许上传图片的最大大小
    private $upload_file_size; // 允许上传文件的最大大小
    private $upload_pic_suffix; // 允许上传的图片后缀
    private $upload_file_suffix; // 允许上传的文件后缀
    private $upload_water_type; // 水印类型
    private $upload_water_text; // 水印文字文本
    private $upload_water_color; // 水印文字颜色
    private $upload_water_size; // 水印文字大小
    private $upload_water_src; //水印图片路径
    private $upload_water_position; //水印显示位置
    private $upload_water_opacity; //水印透明度

    private $pic_ext_arr;//允许上传的图片后缀数组
    private $file_ext_arr;//允许上传的文件后缀数组

    //初始化参数
    public function __construct() {
        $upload_config = \config_helper::get_config_array('upload');

        //允许上传图片的最大大小
        $this->upload_pic_size = $upload_config['upload_pic_size'];
        if(!$this->upload_pic_size){
            $this->upload_pic_size = 2;
        }
        $this->upload_pic_size = $this->upload_pic_size*1024*1024;

        //允许上传文件的最大大小
        $this->upload_file_size = $upload_config['upload_file_size'];
        if(!$this->upload_file_size){
            $this->upload_file_size = 2;
        }
        $this->upload_file_size = $this->upload_file_size*1024*1024;


        //允许上传的图片后缀
        $this->upload_pic_suffix = $upload_config['upload_pic_suffix'];
        if(!$this->upload_pic_suffix){
            $this->upload_pic_suffix = 'jpg|png|gif|';
        }

        $this->pic_ext_arr = explode('|', $this->upload_pic_suffix);


        //允许上传的文件后缀
        $this->upload_file_suffix = $upload_config['upload_file_suffix'];
        if(!$this->upload_file_suffix){
            $this->upload_file_suffix = 'zip|rar|doc|xls|';
        }

        $this->file_ext_arr = explode('|', $this->upload_file_suffix);


        //水印类型，默认文字
        $this->upload_water_type = $upload_config['upload_water_type'];
        if(!$this->upload_water_type){
            $this->upload_water_type = 1;
        }

        //水印文字文本
        $this->upload_water_text = $upload_config['upload_water_text'];
        if(!$this->upload_water_text){
            $this->upload_water_text = 'GoPHP';
        }

        //水印文字颜色
        $this->upload_water_color = $upload_config['upload_water_color'];

        //水印文字大小
        $this->upload_water_size = $upload_config['upload_water_size'];
        if(!$this->upload_water_size){
            $this->upload_water_size = 14;
        }

        //图片水印路径
        $water_attachment_id = $upload_config['upload_water_src'];

        $attachment_info = _uri('attachment', $water_attachment_id);

        if(!$water_attachment_id){
            $this->upload_water_src = './static/images/default_water.png';
        }else{
            $this->upload_water_src = './uploads'.$attachment_info['path'].$attachment_info['save_name'].'.'.$attachment_info['suffix'];
        }

        //水印位置，默认居中
        $this->upload_water_position = $upload_config['upload_water_position'];
        if(!$this->upload_water_position){
            $this->upload_water_position = 5;
        }

        //水印透明度，默认60
        $this->upload_water_opacity = $upload_config['upload_water_opacity'];
        if(!$this->upload_water_opacity){
            $this->upload_water_opacity = 60;
        }
    }

    //图片上传
    public function image($input_name, $has_water = false) {

        $upload = new \Think\Upload(); // 实例化上传类

        $upload->maxSize = $this->upload_pic_size; // 设置图片上传大小

        $upload->exts = $this->pic_ext_arr; // 设置附件上传类型

        $upload->rootPath = './uploads/'; // 设置附件上传根目录
        
        $upload->autoSub = true; //自动生成子目录
        
        $upload->subName = array('date','Y/m/d'); //设置子目录命名规则
        //相对于uploads目录的相对路径
        $upload->savePath =  '/images/';

        $image_info = $upload->uploadOne($_FILES[$input_name]);

        if(!$image_info){
            $return['error'] = 1;
            $return['message'] = $upload->getError();
        }else{
            //将上传图片信息插入附件表中

            //获取原始文件名(不含后缀)
            $orig_name = $image_info['name'];
            $orig_path_info = pathinfo($orig_name);

            //获取上传文件名(不含后缀)
            $save_name = $image_info['savename'];
            $save_path_info = pathinfo($save_name);

            $data['user_id']   = session('admin_id');
            $data['type']      = 'image';
            $data['suffix']    = $image_info['ext'];
        	$data['path']      = $image_info['savepath'];
        	$data['orig_name'] = $orig_path_info['filename'];
        	$data['save_name'] = $save_path_info['filename'];
        	$data['size']      = $image_info['size'];
        	$data['has_thumb'] = $has_thumb;
            $data['add_time']  = date('Y-m-d H:i:s');

        	$attachment_id = M('attachment')->add($data);
        	if($attachment_id){
        		$return['attachment_id'] = $attachment_id;
        	}

        	$image_path = './uploads'.$image_info['savepath'].$image_info['savename'];

        	//引入图片处理类
        	$image = new \Think\Image();

        	$image->open($image_path);

        	$width = $image->width();

        	$height = $image->height();

        	if( $has_water && $this->upload_water_type == 1 ){
        	    //文字水印
        	    $image->open($image_path)->text($this->upload_water_text, './static/fonts/water_font.ttf', $this->upload_water_size, $this->upload_water_color, $this->upload_water_position)->save($image_path);
        	}else if($has_water && $this->upload_water_type == 2 ){
        	    //图片水印
                //print_r($this->upload_water_src);exit;
        	    $image->open($image_path)->water($this->upload_water_src, $this->upload_water_position, $this->upload_water_opacity)->save($image_path);
        	}

        }

        return $return;

    }


    //文件上传
    public function file() {
        $upload = new \Think\Upload(); // 实例化上传类

        $upload->maxSize = $this->upload_file_max_size; // 设置文件上传大小

        $upload->exts = $this->file_ext_arr; // 设置附件上传类型

        $upload->rootPath = './uploads/'; // 设置附件上传根目录
        //相对于uploads目录的相对路径
        $upload->savePath =  '/file/';

        $file_list = $upload->upload();

        if (!$file_list) {

            $return['error'] = 1;

            $return['message'] = $upload->getError();

        } else {
            $k = 0;
            foreach ($file_list as $file_info) {
                $k++;
                $data['type'] = 'file';
                $data['path'] = $file_info['savepath'];
                $data['suffix'] = $file_info['ext'];
                $data['orig_name'] = str_replace('.'.$file_info['ext'], '', $file_info['name']);
                $data['save_name'] = str_replace('.'.$file_info['ext'], '', $file_info['savename']);
                $data['size']      = $file_info['size'];

                $data['user_id'] = session('user_id');
                $data['add_time'] = date('Y-m-d H:i:s', time());

                $attachment_id = M('attachment')->add($data);

                $return[$k-1]['attachment_id'] = $attachment_id;
                $return[$k-1]['input_name']    = $file_info['key'];

            }
        }

        return $return;

    }


}